package com.broadengate.action;



import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.broadengate.form.BalanceForm;
import com.broadengate.model.Atminfo;

import com.broadengate.service.AdminService;


public class BalanceAction extends DispatchAction{
	private AdminService adminService;

	/******
	 * 管理员查看本机余额,并添加余额
	 * 核心功能由调用AdminService对象的方法findAtminfo()，updateAtminfo()实现
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward 对象
	 */
	public ActionForward setBalance(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		System.out.println("BalanceAction 进入");				
		BalanceForm balanceForm = (BalanceForm) form;
		HttpSession session=request.getSession();
		float balanceAdded = Float.parseFloat(balanceForm.getBalanceAdded());	
		System.out.println(balanceAdded);
		Atminfo atminfo=adminService.findAtminfo();
		float formerBalance=atminfo.getBalance();
		atminfo.setBalance(formerBalance+balanceAdded);
		
		try{
			adminService.updateAtminfo(atminfo);	
			session.setAttribute("again","succ");
			return mapping.findForward("success");
		}
		catch (Exception e) {
			return mapping.findForward("fail");
		}
	}

	public ActionForward showBalance(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){		
		try{
			Atminfo atminfo = adminService.findAtminfo();
			Float balance = atminfo.getBalance();
			request.getSession().setAttribute("balance", balance);
			return mapping.findForward("jump");
		}
		catch (Exception e) {
			return mapping.findForward("error");
		}
	}
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}
}
